﻿using System;
using System.IO;
using ReportWizard.Properties;

namespace ReportWizard.Helpers
{
    public class FileSystem
    {
        public static bool CreateDirectory(string directoryPath)
        {
            try
            {
                if (!Directory.Exists(directoryPath))
                    Directory.CreateDirectory(directoryPath);

                return Directory.Exists(directoryPath);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                return false;
            }
        }

        public static string ReadDataBaseScriptFromFile(string pathToFile, string pathToBase)
        {
            try
            {
                if (File.Exists(pathToFile))
                {
                    using (StreamReader encodedFileReader = new StreamReader(pathToFile))
                    {
                        string fileContent = encodedFileReader.ReadToEnd();
                        if (!string.IsNullOrEmpty(fileContent))
                        {
                            fileContent = Crypto.Decrypt(Convert.FromBase64String(fileContent));

                            if (!string.IsNullOrEmpty(fileContent))
                            {
                                string oilReportWizardDbName = ConfigReader.DbName;

                                string dbName = string.Format("{0}.mdf", oilReportWizardDbName);
                                string dbLogName = string.Format("{0}_log.ldf", oilReportWizardDbName);

                                if (!File.Exists(dbName) && !File.Exists(dbLogName))
                                {
                                    fileContent = string.Format(fileContent, oilReportWizardDbName,
                                                                Path.Combine(pathToBase, dbName),
                                                                Path.Combine(pathToBase, dbLogName));

                                    return fileContent;
                                }
                                return string.Empty;
                            }
                            return string.Empty;
                        }
                        return string.Empty;
                    }
                }
                return string.Empty;
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                return string.Empty;
            }
        }

        public static string ReadDataBaseScriptFromResources(string pathToBase)
        {
            try
            {
                string fileContent = Resources.encBase;
                if (!string.IsNullOrEmpty(fileContent))
                {
                    fileContent = Crypto.Decrypt(Convert.FromBase64String(fileContent));

                    if (!string.IsNullOrEmpty(fileContent))
                    {
                        string oilReportWizardDbName = ConfigReader.DbName;

                        string dbName = string.Format("{0}.mdf", oilReportWizardDbName);
                        string dbLogName = string.Format("{0}_log.ldf", oilReportWizardDbName);

                        if (!File.Exists(dbName) && !File.Exists(dbLogName))
                        {
                            return string.Format(fileContent, oilReportWizardDbName,
                                                        Path.Combine(pathToBase, dbName),
                                                        Path.Combine(pathToBase, dbLogName));
                        }
                        return string.Empty;
                    }
                    return string.Empty;
                }
                return string.Empty;
                    
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                return string.Empty;
            }
        }

        public static void WriteDataBaseScript(string pathToFile, string sqlScript)
        {
            try
            {
                if (!string.IsNullOrEmpty(pathToFile) && !File.Exists(pathToFile))
                {
                    using (StreamWriter sw = new StreamWriter(pathToFile, false))
                    {
                        sw.Write(sqlScript);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }

        public static string ReadUserScriptFromResources()
        {
            try
            {
                string fileContent = Resources.encUser;
                if (!string.IsNullOrEmpty(fileContent))
                {
                    fileContent = Crypto.Decrypt(Convert.FromBase64String(fileContent));

                    if (!string.IsNullOrEmpty(fileContent))
                    {
                        string dbName = ConfigReader.DbName;
                        string user = ConfigReader.DbUserLogin;
                        string password = ConfigReader.DbUserPassword;

                        return string.Format(fileContent, dbName, user, password);
                    }
                    return string.Empty;
                }
                return string.Empty;
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                return string.Empty;
            }
        }

        public static void EncryptFile(string pathToFile)
        {
            try
            {
                if (!string.IsNullOrEmpty(pathToFile) && File.Exists(pathToFile))
                {
                    using (StreamReader sr = new StreamReader(pathToFile))
                    {
                        string fileContent = sr.ReadToEnd();
                        if (!string.IsNullOrEmpty(fileContent))
                        {
                            fileContent = Convert.ToBase64String(Crypto.Encrypt(fileContent));

                            string encryptedFileName = string.Format("{0}.encrypted", Path.GetFileNameWithoutExtension(pathToFile));

                            string encryptedFilePath = Path.Combine(Path.GetDirectoryName(pathToFile), encryptedFileName);

                            if (!File.Exists(encryptedFilePath))
                            {
                                using (StreamWriter sw = new StreamWriter(encryptedFilePath, false))
                                {
                                    sw.Write(fileContent);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }

        private void CreateEmbeddedResourceCopy()
        {
            const string defaultNamespace = "myDefaultNamespace";
            const string fileName = "filename.ext";
            using (Stream source = this.GetType().Assembly.GetManifestResourceStream(defaultNamespace + "." + fileName))
            {
                using (FileStream target = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Write))
                {
                    byte[] buffer = new byte[16384];  // buffer size
                    int read;
                    while (source != null && (read = source.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        target.Write(buffer, 0, read);
                    }
                }
            }
        }
    }
}
